﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;

namespace sands
{
    public abstract class SandsPrintDocument:PrintDocument
    {
        protected enum AlignType { Left, Centre, Right };
        protected Font _font;
        protected int rowNumber = 0;
        protected uint pageNumber=1;
        protected String date = String.Empty;
        
        protected String[] columnsNames;
        protected Rectangle[] columns;

        protected Graphics gdiPage;

        public Font Font
        {
            get
            {
                return _font; 
            }
            set 
            {
                _font = value; 
            }
        }

        public SandsPrintDocument(String date)
        {
            this.date = date;
        }

        protected override void OnBeginPrint(PrintEventArgs e)
        {
            rowNumber = 0;
            base.OnBeginPrint(e);
            Font = new Font("Verdana", 9);
            pageNumber = 1;
        }

        protected override void OnEndPrint(PrintEventArgs e)
        {
            base.OnEndPrint(e);
            Font.Dispose();
        }

        protected override void OnPrintPage(PrintPageEventArgs e)
        {
            base.OnPrintPage(e);

            gdiPage = e.Graphics;
            int topMargin = e.MarginBounds.Top;

            if(pageNumber.Equals(1)) //draw title if first page
                DrawTitle(ref topMargin);

            DrawHeaders(ref topMargin); //draw header row
            e.HasMorePages=DrawTable(ref topMargin); //draw table;
            gdiPage.DrawString("Strona " + pageNumber, Font, Brushes.Black, Align("Strona " + pageNumber, new Rectangle(e.MarginBounds.Left, e.MarginBounds.Bottom, e.MarginBounds.Width, 100), AlignType.Centre));
            ++pageNumber; //increase page number
        }
   
        protected Rectangle Align(String text,Rectangle space, AlignType align)
        {
            Rectangle res = space;
            switch (align)
            {
                case AlignType.Left:
                    break;
                case AlignType.Right:
                    res.Location = new Point(res.Right - (int)gdiPage.MeasureString(text, Font).Width, res.Top);
                    break;
                case AlignType.Centre:
                    res.Location = new Point((res.Right - (int)gdiPage.MeasureString(text, Font).Width)/2+50, res.Top);
                    break;
            }
            return res;
        }

        protected abstract void DrawTitle(ref int top);
        protected abstract void DrawHeaders(ref int top);
        protected abstract bool DrawTable(ref int top);
    }
}
